package models

import (
	"github.com/jinzhu/gorm"
	"kop/modules/world_boss/configuration"
	"kop/modules/world_boss/entity"
	"kop/util/date"
	"time"
)

func GetLittleBoss(dB *gorm.DB, roleID int32) (*entity.LittleBoss, error) {

	var littleBoss = &entity.LittleBoss{RoleID: roleID}

	if err := dB.FirstOrCreate(littleBoss).Error; err == nil {

		if littleBoss.Date != date.Date(time.Now()) {

			littleBoss.Date = date.Date(time.Now())
			littleBoss.IDsMap = make(map[int32]int32)
			littleBoss.Level = 1

			var conf = configuration.GetLittleBoss(littleBoss.Level)
			littleBoss.HP = conf.BossHP
		}

		return littleBoss, nil
	} else {
		return nil, err
	}
}

func SaveLittleBoss(dB *gorm.DB, littleBoss *entity.LittleBoss) error {

	return dB.Save(littleBoss).Error
}
